<template>
  <div class="app-container">
    <el-form :model="form" ref="form" :inline="true">
      <el-form-item label="计划单号" prop="planNo">
        <el-input v-model="form.planNo" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="计划名称" prop="planName">
        <el-input v-model="form.planName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input v-model="form.productName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" icon="el-icon-plus" plain v-hasPermi="['ims:producePlan:add']" @click="updateRow(0,{})">
      新增
    </el-button>
    <el-table
      v-loading="loading"
      :data="list"
    >
      <el-table-column type="index" label="序号" align="center">
        <template slot-scope="scope">
          <span>{{ (form.pageNum - 1) * form.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="planNo" label="生产单号" align="center" width="180">
        <template slot-scope="scope">
          <el-link type="primary" @click="updateRow(1,scope.row)">{{ scope.row.planNo }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="planName" label="计划名称" align="center" width="180"/>
      <el-table-column prop="reOrderNo" label="关联单号" align="center" width="150"/>
      <el-table-column prop="fileName" label="cad图纸" align="center" width="150"/>
      <el-table-column prop="status" label="状态" align="center" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_plan_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column prop="taskSourceType" label="生产来源" align="center" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ims_produce_type" :value="scope.row.taskSourceType"/>
        </template>
      </el-table-column>
      <el-table-column prop="productName" label="商品名称" align="center"/>
      <!--      <el-table-column prop="skuAttributeVoList" label="商品规格" align="center">-->
      <!--        <template slot-scope="scope">-->
      <!--            <div v-for="val in scope.row.skuAttributeVoList" style="display: flex;justify-content: center;">-->
      <!--              <span> {{ val.attributeName }}：</span>-->
      <!--              <span> {{ val.attributeValue }}{{ val.unit }}</span>-->
      <!--            </div>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column prop="quantity" label="数量" align="center" width="100"/>
      <el-table-column prop="priority" label="优先级" align="center" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ims_priority_level" :value="scope.row.priority"/>
        </template>
      </el-table-column>
      <el-table-column prop="delay" label="是否延期" align="center" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_order_delay" :value="scope.row.delay"/>
        </template>
      </el-table-column>
      <el-table-column prop="startTime" label="开始/结束时间" align="center" width="170">
        <template slot-scope="scope">
          <div v-if="scope.row.startTime">
            {{ scope.row.startTime }}至{{ scope.row.endTime }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="updateRow(1,scope.row)"
          >详情
          </el-button>
          <el-button
            type="text"
            v-hasPermi="['ims:producePlan:download']"
            @click="downloadFile(scope.row)"
          >下载
          </el-button>
          <el-button
            type="text"
            v-hasPermi="['ims:producePlan:update']"
            v-if="scope.row.status==1"
            @click="updateRow(2,scope.row)"
          >修改
          </el-button>
          <el-button
            type="text"
            v-if="scope.row.status==1"
            v-hasPermi="['ims:producePlan:share']"
            @click="updateRow(3,scope.row)"
          >分配
          </el-button>
          <el-button
            type="text"
            v-hasPermi="['ims:producePlan:delete']"
            v-if="scope.row.status!=3&&scope.row.status!=4"
            @click="updateRow(4,scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="total"
      :page.sync="form.pageNum"
      :limit.sync="form.pageSize"
      @pagination="getList"
    />
    <updateDialog ref="updateDialog" @ok="handleQuery"></updateDialog>
  </div>
</template>

<script>
import updateDialog from "./updateDialog.vue";
import {deleteProducePlan, listProducePlan} from "../../../api/produceMng";

export default {
  components: {updateDialog},
  dicts: ['ims_produce_order_status', 'ims_produce_type', 'ims_priority_level', 'sys_order_delay', 'sys_plan_status'],
  data() {
    return {
      loading: false,
      list: [],
      form: {
        planNo: "",
        planName: "",
        productName: "",
        pageNum: 1,
        pageSize: 10
      },
      total: 0
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.handleQuery()
    },
    handleQuery() {
      this.form.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('form')
      this.handleQuery()
    },
    getList() {
      this.loading = true
      listProducePlan(this.form).then(res => {
        this.list = res.rows
        this.total = res.total
      }).finally(e => {
        this.loading = false
      })
    },
    downloadFile(row) {
      if (!row.fileUrl) {
        this.$message.error('暂无文件')
        return
      }
      window.open(row.fileUrl)
    },
    updateRow(flag, row) {
      let form = JSON.parse(JSON.stringify(row))
      if (flag != 4) {
        this.$refs.updateDialog.show(flag, form)
      }
      if (flag == 4) {
        this.$modal.confirm('是否确定删除').then(res => {
          deleteProducePlan(form.id).then(res => {
            this.handleQuery()
            this.$message.success('删除成功')
          })
        })
      }
    }
  }
}
</script>
